package com.example.opengl.camera.filter

import android.content.Context
import android.opengl.GLES20
import android.opengl.GLES20.*
import com.android.baselibrary.app.MainApplication
import com.example.opengl.R
import com.example.opengl.base.BufferHelper
import com.example.opengl.base.ShaderHelper
import com.example.opengl.base.TextResourceReader
import java.nio.FloatBuffer

/**
 * @author: Mr.You
 * @create: 2023-07-18 16:25
 * @description:
 **/
open class BaseFilter private constructor() {
    //顶点着色器id
    private var mVertexSourceId: Int = -1

    //片元着色器id
    private var mFragmentSourceId: Int = -1

    //顶点坐标数据缓冲区
    protected var mVertexBuffer: FloatBuffer? = null

    //纹理坐标数据缓冲区
    protected var mTextureBuffer: FloatBuffer? = null

    //着色器程序id
    protected var mProgramId: Int = -1

    //顶点着色器，顶点位置下标
    protected var vPosition: Int = -1

    //顶点着色器，纹理坐标
    protected var vCoord: Int = -1

    //顶点着色器，变换矩阵
    protected var vMatrix: Int = -1

    //片元着色器，采样器
    protected var vTexture: Int = -1

    //宽度
    protected var mWidth: Int = 0

    //高度
    protected var mHeight: Int = 0

    constructor(vertexSourceId: Int = R.raw.base_vertex, fragmentSourceId: Int) : this() {
        this.mVertexSourceId = vertexSourceId
        this.mFragmentSourceId = fragmentSourceId
        //初始化顶点坐标系

        val VERTEX = floatArrayOf(
            -1f, -1f,
            1f, -1f,
            -1f, 1f,
            1f, 1f
        )
        mVertexBuffer = BufferHelper.getFloatBuffer(VERTEX)

        //初始化纹理坐标系
        val TEXTURE = floatArrayOf(
            0.0f, 0.0f,
            1.0f, 0.0f,
            0.0f, 1.0f,
            1.0f, 1.0f,
        )
        mTextureBuffer = BufferHelper.getFloatBuffer(TEXTURE)
        init()
    }

    private fun init() {

        val vertexSource = TextResourceReader.readTextFileFormResource(MainApplication.mainApplication, mVertexSourceId)
        val fragmentSource = TextResourceReader.readTextFileFormResource(MainApplication.mainApplication, mFragmentSourceId)

        val vertexShaderId = ShaderHelper.compileVertexShader(vertexSource)
        val fragmentShaderId = ShaderHelper.compileFragmentShader(fragmentSource)
        mProgramId = ShaderHelper.linkProgram(vertexShaderId, fragmentShaderId)

        //删除顶点着色器和片元着色器id
        glDeleteShader(vertexShaderId)
        glDeleteShader(fragmentShaderId)

        vPosition = glGetAttribLocation(mProgramId,"vPosition")
        vCoord = glGetAttribLocation(mProgramId,"vCoord")
        vMatrix = glGetUniformLocation(mProgramId,"vMatrix")

        vTexture = glGetUniformLocation(mProgramId,"vTexture")

    }
    open fun onReady(width:Int,height:Int){
        mWidth = width
        mHeight = height
    }
   open fun  onDrawFrame(textureId:Int):Int{

        //设置窗口大小
        glViewport(0,0,mWidth,mHeight)
        //调用着色器程序
        glUseProgram(mProgramId)
        //todo 绘制
        //顶点坐标赋值
        mVertexBuffer?.position(0)
        glVertexAttribPointer(vPosition,2, GL_FLOAT,false,0,mVertexBuffer)
        glEnableVertexAttribArray(vPosition)

        //纹理坐标赋值
        mTextureBuffer?.position(0)
        glVertexAttribPointer(vCoord,2, GL_FLOAT,false,0,mTextureBuffer)
        glEnableVertexAttribArray(vCoord)
        //激活纹理
        glActiveTexture(GL_TEXTURE0)
        //绑定纹理
        glBindTexture(GL_TEXTURE_2D,textureId)
        //传递采样器
        glUniform1i(vTexture,0)
        //开始绘制
        glDrawArrays(GL_TRIANGLE_STRIP,0,4)


       glDisableVertexAttribArray(vPosition)
       glDisableVertexAttribArray(vCoord)
       glBindTexture(GL_TEXTURE_2D,0)


        return textureId
    }

}